package loc.dd.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;

import loc.dd.entity.Abilities;
import loc.dd.service.AbilityService;
import net.sf.json.JSONObject;

/**
*@author 作者：huyaqi
*@version 创作时间:2023年11月29日上午9:15:48
*
*/

@RestController
@RequestMapping("/ability")
public class AbilityController {
	private static Logger logger =LoggerFactory.getLogger(AbilityController.class);
	
	@Autowired
	private AbilityService abilityService;
	
	/**
	 * 查找全部能力
	 * @param request
	 * @param response
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "getnl" , method = RequestMethod.GET)
	public JSONArray getnl(HttpServletRequest request,HttpServletResponse response) {
		JSONArray jo = new JSONArray();
		List<Map<String, Object>> ab= new ArrayList<Map<String, Object>> ();
		ab = abilityService.getnl();
		System.out.println(ab);
		jo.add(ab);
		return jo;
    }
	
	/**
	 * 编辑能力
	 */
	@CrossOrigin
	@RequestMapping(value = "editnl" , method = RequestMethod.POST)
	public Map<String,Object> editnl(@RequestBody JSONObject params,HttpServletRequest request,HttpServletResponse response) {
		JSONObject jsonx = (JSONObject) params.get("data");
		Map<String, Object> map = new HashMap<>();
		String abilityname=(String)jsonx.get("abilityname");
		String description=(String)jsonx.get("description");
		Abilities ab = new Abilities();
		System.out.println("editnl");
		if("" != jsonx.get("id") && !"".equals(jsonx.get("id"))){
			int ids = (int) jsonx.get("id");
			Abilities abilities = abilityService.getonenl(ids);
			
			int a = abilityService.checkshifounl(abilityname);
			if(a>0 && !(abilities.getAbilityname()).equals(abilityname)) {
				map.put("status", "407");
				return map;
			}
			ab.setId(ids);
			ab.setAbilityname(abilityname);
			ab.setDescription(description);
			abilityService.editnl(ab);
			map.put("status", "200");
			return map; 
		}else {
			ab.setAbilityname(abilityname);
			ab.setDescription(description);
			int a = abilityService.checkshifounl(abilityname);
			if(a>0) {
				map.put("status", "407");
				return map;
			}
			abilityService.addnl(ab);
			map.put("status", "200");
			return map; 
		}
    }
	
	/**
	 * 删除能力
	 * @param params
	 * @param request
	 * @param response
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "delnl" , method = RequestMethod.POST)
	public Map<String,Object> delnl(@RequestBody JSONObject params,HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		Abilities ab = new Abilities();
		if(null != params.get("id") && !"".equals(params.get("id"))) {
			int id = (int) params.get("id");
			int a = abilityService.checknl(id);
			if(a>0) {
				map.put("status", "407");
				return map;
			}
			abilityService.delnl(id);
			map.put("status", "200");
			return map; 
		}
		else {
			map.put("status", "407");
		}
		
		
		return map; 
    }
	
	
}
